Skip to content

feat(complete): group options by tag (in zsh)#6334

Open
bobrippling wants to merge 2 commits intoclap-rs:masterfrom
bobrippling:feat/zsh-completion-grouping
Open

feat(complete): group options by tag (in zsh)#6334
bobrippling wants to merge 2 commits intoclap-rs:masterfrom
bobrippling:feat/zsh-completion-grouping

Conversation

@bobrippling
Copy link
Copy Markdown

@bobrippling bobrippling commented Apr 3, 2026

This allows zsh to group completions by whether they're global, and then by their tag. For example:

$ dynamic -<tab>
completing "global" options
-v  -- --verbose
completing "Options" options
-F  -- --format
-h  -- Print help
-i  -- --input

Closes #6320

Outstanding Questions

Options name

completing "Options" options doesn't seem particulary nice, should we filter out the default "Options" tag?

arg.get_help_heading()
.unwrap_or("Options")
.to_owned()

when generating the completions in shells.rs

Tests

My new test is currently failing because it gets the zsh initial login setup:

This is the Z Shell configuration function for new users,
zsh-newuser-install.
You are seeing this message because you have no zsh startup files
(the files .zshenv, .zprofile, .zshrc, .zlogin in the directory
/tmp/.tmpQxncRl).  This function can help you with a few settings that should
make your use of the shell easier.
[...]
Aborting.
The function will be run again next time.  To prevent this, execute:
  touch /tmp/.tmpQxncRl/.zshrc

I'm not sure why my test gets this but the others don't

@bobrippling bobrippling force-pushed the feat/zsh-completion-grouping branch 11 times, most recently from e5a4b4d to 629cac1 Compare April 11, 2026 16:26
@bobrippling bobrippling marked this pull request as ready for review April 11, 2026 16:37
@bobrippling bobrippling changed the title feat(complete): group options by tag (in zsh) feat(complete): group options by global state and tag (in zsh) Apr 11, 2026
@bobrippling bobrippling force-pushed the feat/zsh-completion-grouping branch 4 times, most recently from 65f0b45 to b161c88 Compare April 11, 2026 19:35
Comment thread clap_complete/src/engine/candidate.rs Outdated
@bobrippling bobrippling force-pushed the feat/zsh-completion-grouping branch from b161c88 to 0609ae5 Compare April 13, 2026 17:11
@epage epage changed the title feat(complete): group options by global state and tag (in zsh) feat(complete): group options by tag (in zsh) Apr 13, 2026
Comment thread clap_complete/tests/testsuite/zsh.rs Outdated
Comment on lines 388 to +402
if [[ "$value" == */ ]]; then
local dir_no_slash="${value%/}"
if [[ "$completion" == *:* ]]; then
local desc="${completion#*:}"
if [[ "$value" == *:* ]]; then
local desc="${value#*:}"
dirs+=("$dir_no_slash:$desc")
else
dirs+=("$dir_no_slash")
fi
else
other+=("$completion")
if (( ${+tag_map["$tag"]} )); then # key exists?
tag_map["$tag"]+=$'\n'"$value"
else
tag_map["$tag"]="$value"
fi
fi
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we aren't getting the / handling if a tag is present?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The opposite in fact - if a / is there, we'll handle it (since we test that first) and only perform the option grouping for remaining options

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Items with / should still have grouping though

}
write!(
buf,
"{}:",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is : is in a tag?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we have two options:

  • Pick a different separator - but what if that separator is in the tag?
  • Avoid : in tags, either by filtering them out at this point, or making it part of the tag requirement

What do you think?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pick an unlikely separator, like _SEP_

Comment thread clap_complete/src/env/shells.rs
@bobrippling bobrippling force-pushed the feat/zsh-completion-grouping branch 2 times, most recently from eb40761 to eeee9ab Compare May 1, 2026 17:06
@bobrippling bobrippling force-pushed the feat/zsh-completion-grouping branch from eeee9ab to db855d8 Compare May 1, 2026 17:15
@bobrippling bobrippling force-pushed the feat/zsh-completion-grouping branch from db855d8 to 9e85b97 Compare May 1, 2026 17:17
% zstyle ':completion:*' group-name ''
% zstyle ':completion:*:descriptions' format '%d'
% exhaustive -
"Options" options
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is redundant. In --help, we just list the header. We should likely do the same here as well.

Comment on lines +451 to +452
"zstyle ':completion:*' group-name ''",
"zstyle ':completion:*:descriptions' format '%d'",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these needed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Zsh completion tagging / grouping

3 participants